package dialogos;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import paneles.PanelProductos;
import accesoADatos.AccesoADatos;
import datos.Producto;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ModifProdDialog extends javax.swing.JDialog {
	private PanelProductos panelProductos;
	private JPanel jPanel1;
	private JComboBox jComboBox;
	@SuppressWarnings("unchecked")
	private Hashtable hash;
	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				ModifProdDialog inst = new ModifProdDialog(frame);
				inst.setVisible(true);
			
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	public ModifProdDialog(JFrame frame) {
		super(frame,"Modificar Producto",true);
		hash= new Hashtable();
		initGUI();
		
	}
	
	@SuppressWarnings("unchecked")
	private void initGUI() {
		try {
			{
				panelProductos = new PanelProductos();
				getContentPane().add(panelProductos, BorderLayout.CENTER);
				panelProductos.setPreferredSize(new java.awt.Dimension(60, 42));
				panelProductos.getBAceptar().addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						bAceptarActionPerformed(evt);
					}
				});
				panelProductos.getBCancelar().addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						bCancelarActionPerformed(evt);
					}
				});
			}
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1, BorderLayout.NORTH);
				jPanel1.setPreferredSize(new java.awt.Dimension(327, 36));
				List l=AccesoADatos.getInstance().getProductos();
				for (int i=0; i<l.size(); i++){
					hash.put(l.get(i).toString(),l.get(i));
				}
				{
					ComboBoxModel jComboBoxModel = 
						new DefaultComboBoxModel(
								l.toArray());
					jComboBox = new JComboBox();
					jPanel1.add(jComboBox);
					jComboBox.setModel(jComboBoxModel);
					Producto p = (Producto)hash.get(jComboBox.getSelectedItem().toString());
					this.panelProductos.setProducto(p);
					jComboBox.setPreferredSize(new java.awt.Dimension(135, 22));
					jComboBox.setBackground(new java.awt.Color(255,255,255));
					jComboBox.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
					jComboBox.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
							ModifProdDialog.this.panelProductos.setProducto((Producto)hash.get(jComboBox.getSelectedItem().toString()));
						}
					});
				}
			}
			pack();
			this.setSize(335, 387);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void bCancelarActionPerformed(ActionEvent evt) {
		this.dispose();
	}
	
	private void bAceptarActionPerformed(ActionEvent evt) {
		Producto p = this.panelProductos.getProducto();
		Producto p1 = (Producto)hash.get(this.jComboBox.getSelectedItem().toString());
		p1.setCantidad(p.getCantidad());
		p1.setCantidaMinima(p.getCantidaMinima());
		p1.setCodigoBarra(p.getCodigoBarra());
		p1.setDescripcion(p.getDescripcion());
		p1.setMarca(p.getMarca());
		p1.setNombre(p.getNombre());
		p1.setPrecioCosto(p.getPrecioCosto());
		p1.setPrecioVenta(p.getPrecioVenta());
		p1.setProveedor(p.getProveedor());
		AccesoADatos.getInstance().setObjeto(p1);
		this.dispose();
	}

}
